// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  traceUser: true,
  env:'aaa111-9gh9fp5ieac4de5d'
})

const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
    const tripId = event.tripId
    var tripDaseInfo = {}
    //获取基本信息
    await db.collection('trip').where({
        _id: tripId
    }).get().then(res => {
        tripDaseInfo = res.data[0]
        //获取需要修改的两个元素：endtime和days
        var tripEndTime = tripDaseInfo.tripEndTime
        var days = tripDaseInfo.days
        console.log(tripEndTime)
        console.log(days)
        //更新结束时间
        tripEndTime = tripEndTime + (1 * 24 * 60 * 60 * 1000)
        //新建一个新的day
        db.collection('tripDays').add({
            data: {}
        }).then(res => {
            days.push(res._id)
            console.log(days)
            //更新值
            db.collection('trip').where({
                _id: tripId
            }).update({
                data: {
                    tripEndTime: tripEndTime,
                    days: days
                }
            }).catch(err => {
                console.error(err)
            })
        }).catch(err => {
            console.error(err)
        })
    })
    return true
}